<?php

namespace app\common\service;

use think\response\Json;

class JsonService
{
    /**
     * 接口返回消息
     * @param int $code 状态码
     * @param string $msg 消息
     * @param mixed $data 数据
     * @return Json json
     */
    public static function result(int $code, string $msg = '操作成功', $data = null): Json
    {
        $res = compact('code', 'msg', 'data');
        return json($res);
    }

    public static function success(): Json
    {
        return self::result(200);
    }

    public static function successWithMsg(string $msg): Json
    {
        return self::result(200, $msg);
    }

    public static function successWithData($data): Json
    {
        return self::result(200, '操作成功', $data);
    }

    public static function successWithMsgAndData(string $msg, $data): Json
    {
        return self::result(200, $msg, $data);
    }

    public static function error(): Json
    {
        return self::result(500, '操作失败');
    }

    public static function errorWithMsg(string $msg): Json
    {
        return self::result(500, $msg);
    }

    public static function errorWithMsgAndData(string $msg, $data): Json
    {
        return self::result(500, $msg, $data);
    }

    public static function warn(string $msg): Json
    {
        return self::result(600, $msg);
    }

    public static function warnWithMsgAndData(string $msg, $data): Json
    {
        return self::result(600, $msg, $data);
    }

    public static function toAjax(int $rows): Json
    {
        return $rows > 0 ? self::success() : self::error();
    }
}